home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / BorderPanel.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  27.9 KB  |  1,071 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Panel;
  4. import java.awt.Insets;
  5. import java.awt.Component;
  6. import java.awt.Dimension;
  7. import java.awt.LayoutManager;
  8. import java.awt.Color;
  9. import java.awt.FontMetrics;
  10. import java.awt.Graphics;
  11. import java.awt.Label;
  12. import java.awt.Font;
  13.  
  14. //    01/29/97        TWB    Integrated changes from Macintosh
  15.  
  16. /**
  17.  * BorderPanel is a panel component that has an optional border 
  18.  * and optional text title.
  19.  * @version 1.0, Nov 26, 1996
  20.  * @author Symantec
  21.  */
  22.  
  23. public class BorderPanel
  24.     extends    Panel
  25.     implements AlignStyle,
  26.                BevelStyle
  27. {
  28.     /**
  29.      * Label x-coordinate padding constant.
  30.      */
  31.     protected static final int labelpadx = 10;
  32.     /**
  33.      * Label x-coordinate inset constant.
  34.      */
  35.     protected static final int labelipadx = 4;
  36.  
  37.     /**
  38.      * The border color.
  39.      */
  40.     protected Color borderColor;
  41.     /**
  42.      * The border label color.
  43.      */
  44.     protected Color labelColor;
  45.     /**
  46.      * The top border padding amount.
  47.      */
  48.     protected int padtop;
  49.     /**
  50.      * The bottom border padding amount.
  51.      */
  52.     protected int padbottom;
  53.     /**
  54.      * The left border padding amount.
  55.      */
  56.     protected int padleft;
  57.     /**
  58.      * The right border padding amount.
  59.      */
  60.     protected int padright;
  61.     /**
  62.      * The side inset border padding amount.
  63.      */
  64.     protected int ixPad;
  65.     /**
  66.      * The top inset border padding amount.
  67.      */
  68.     protected int iyPadTop;
  69.     /**
  70.      * The bottom inset border padding amount.
  71.      */
  72.     protected int iyPadBottom;
  73.     /**
  74.      * The border style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, BEVEL_NONE.
  75.      * @see BevelStyle#BEVEL_RAISED
  76.      * @see BevelStyle#BEVEL_LOWERED
  77.      * @see BevelStyle#BEVEL_LINE
  78.      * @see BevelStyle#BEVEL_NONE
  79.      */
  80.     protected int style;
  81.     /**
  82.      * The current border label.
  83.      */
  84.     protected String label;
  85.     /**
  86.      * The border label alignment: ALIGN_LEFT, ALIGN_CENTERED, ALIGN_RIGHT.
  87.      * @see AlignStyle#ALIGN_LEFT
  88.      * @see AlignStyle#ALIGN_CENTERED
  89.      * @see AlignStyle#ALIGN_RIGHT
  90.      */
  91.     protected int labelAlignment;
  92.     /**
  93.      * The internal border insets.
  94.      */
  95.     protected Insets internalInsets;
  96.     /**
  97.      * The panel within the border.
  98.      */
  99.     protected Panel panel;
  100.     /**
  101.      * The overall size before sizepanel().
  102.      */
  103.     Dimension oldSize = new Dimension();
  104.  
  105.     /**
  106.      * Constructs a new default BorderPanel.
  107.      */
  108.     public BorderPanel()
  109.     {
  110.         this(null, ALIGN_CENTERED, BEVEL_LINE);
  111.     }
  112.  
  113.     /**
  114.      * Constructs a new default BorderPanel with the specified beveled border style.
  115.      * It is constructed with the ALIGN_CENTERED label alignment style.
  116.      * @param style desired beveled border style
  117.      * @see BevelStyle#BEVEL_RAISED
  118.      * @see BevelStyle#BEVEL_LOWERED
  119.      * @see BevelStyle#BEVEL_LINE
  120.      * @see BevelStyle#BEVEL_NONE
  121.      */
  122.     public BorderPanel(int style)
  123.     {
  124.         this(null, ALIGN_CENTERED, style);
  125.     }
  126.  
  127.     /**
  128.      * Constructs a new default BorderPanel with the specified border label.
  129.      * It is constructed with the ALIGN_CENTERED and BEVEL_LINE styles.
  130.      */
  131.     public BorderPanel(String s)
  132.     {
  133.         this(s, ALIGN_CENTERED, BEVEL_LINE);
  134.     }
  135.  
  136.     /**
  137.      * Constructs a new default BorderPanel with the specified border label
  138.      * and given alignment.
  139.      * @param label border panel label string
  140.      * @param alignment alignment of label string
  141.      * @see AlignStyle#ALIGN_LEFT
  142.      * @see AlignStyle#ALIGN_CENTERED
  143.      * @see AlignStyle#ALIGN_RIGHT
  144.      */
  145.     public BorderPanel(String s, int alignment)
  146.     {
  147.         this(s, alignment, BEVEL_LINE);
  148.     }
  149.  
  150.     /**
  151.      * Constructs a new default BorderPanel with the specified border label
  152.      * and given alignment and border styles.
  153.      * @param label border panel label string
  154.      * @param alignment alignment of label string
  155.      * @param style border style
  156.      * @see AlignStyle#ALIGN_LEFT
  157.      * @see AlignStyle#ALIGN_CENTERED
  158.      * @see AlignStyle#ALIGN_RIGHT
  159.      * @see BevelStyle#BEVEL_RAISED
  160.      * @see BevelStyle#BEVEL_LOWERED
  161.      * @see BevelStyle#BEVEL_LINE
  162.      * @see BevelStyle#BEVEL_NONE
  163.      */
  164.     public BorderPanel(String s, int alignment, int style)
  165.     {
  166.         borderColor = Color.black;
  167.         labelColor  = Color.black;
  168.         padleft     = 6;
  169.         padright    = 6;
  170.         padtop      = 10;
  171.         padbottom   = 6;
  172.         ixPad       = 4;
  173.         iyPadTop    = 2;
  174.         iyPadBottom = 7;
  175.         
  176.         label = (s != null && s.length() == 0) ? null : s;
  177.         labelAlignment = alignment;
  178.         internalInsets = new Insets(10, 10, 10, 10);
  179.         this.style     = style;
  180.         setLayout(null);
  181.         super.add(panel = new Panel());
  182.         sizepanel(true);
  183.     }
  184.  
  185.     /**
  186.      * Tells this component that it has been added to a container.
  187.      * This is a standard Java AWT method which gets called by the AWT when 
  188.      * this component is added to a container. Typically, it is used to 
  189.      * create this component's peer. 
  190.      * It is overridden here to resize self after peer created.
  191.      *
  192.      * @see java.awt.Container#removeNotify
  193.      */
  194.     public void addNotify()
  195.     {
  196.         super.addNotify();
  197.         sizepanel(true);
  198.     }
  199.     
  200.     /**
  201.      * Sets the top border padding amount.
  202.      * @param p top border pad amount
  203.      * @see #getPaddingTop
  204.      */
  205.     public void setPaddingTop(int p)
  206.     {
  207.         padtop = p;
  208.         sizepanel(true);
  209.         invalidate();
  210.     }
  211.  
  212.     /**
  213.      * Gets the current top border pad amount.
  214.      * @return top border pad amount
  215.      * @see #setPaddingTop
  216.      */
  217.     public int getPaddingTop()
  218.     {
  219.         return padtop;
  220.     }
  221.  
  222.     /**
  223.      * Sets the bottom border padding amount.
  224.      * @param p bottom border pad amount
  225.      * @see #getPaddingBottom
  226.      */
  227.     public void setPaddingBottom(int p)
  228.     {
  229.         padbottom = p;
  230.         sizepanel(true);
  231.         invalidate();
  232.     }
  233.  
  234.     /**
  235.      * Gets the current bottom border pad amount.
  236.      * @return bottom border pad amount
  237.      * @see #setPaddingBottom
  238.      */
  239.     public int getPaddingBottom()
  240.     {
  241.         return padbottom;
  242.     }
  243.  
  244.     /**
  245.      * Sets the left border padding amount.
  246.      * @param p left border pad amount
  247.      * @see #getPaddingLeft
  248.      */
  249.     public void setPaddingLeft(int p)
  250.     {
  251.         padleft = p;
  252.         sizepanel(true);
  253.         invalidate();
  254.     }
  255.  
  256.     /**
  257.      * Gets the current left border pad amount.
  258.      * @return int - left border pad amount
  259.      * @see #setPaddingLeft
  260.      */
  261.     public int getPaddingLeft()
  262.     {
  263.         return padleft;
  264.     }
  265.  
  266.     /**
  267.      * Sets the right border padding amount.
  268.      * @param p right border pad amount
  269.      * @see #getPaddingRight
  270.      */
  271.     public void setPaddingRight(int p)
  272.     {
  273.         padright = p;
  274.         sizepanel(true);
  275.         invalidate();
  276.     }
  277.  
  278.     /**
  279.      * Gets the current right border pad amount.
  280.      * @return right border pad amount
  281.      * @see #setPaddingRight
  282.      */
  283.     public int getPaddingRight()
  284.     {
  285.         return padright;
  286.     }
  287.  
  288.     /**
  289.      * Sets the top inset border padding amount.
  290.      * @param t top inset border pad amount
  291.      * @see #getIPadTop
  292.      */
  293.     public void setIPadTop(int t)
  294.     {
  295.         iyPadTop = t;
  296.         sizepanel(true);
  297.         invalidate();
  298.     }
  299.  
  300.     /**
  301.      * Sets the bottom inset border padding amount.
  302.      * @param b bottom inset border pad amount
  303.      * @see #getIPadBottom
  304.      */
  305.     public void setIPadBottom(int b)
  306.     {
  307.         iyPadBottom = b;
  308.         sizepanel(true);
  309.         invalidate();
  310.     }
  311.  
  312.     /**
  313.      * Gets the current top inset border padding amount.
  314.      * @return top inset border pad amount
  315.      * @see #setIPadTop
  316.      */
  317.     public int getIPadTop()
  318.     {
  319.         return iyPadTop;
  320.     }
  321.  
  322.     /**
  323.      * Sets the side inset border padding amount.
  324.      * @param s side inset border pad amount
  325.      * @see #getIPadSides
  326.      */
  327.     public void setIPadSides(int s)
  328.     {
  329.         ixPad = s;
  330.         sizepanel(true);
  331.         invalidate();
  332.     }
  333.  
  334.     /**
  335.      * Gets the current bottom inset border padding amount.
  336.      * @return bottom inset border pad amount
  337.      * @see #setIPadBottom
  338.      */
  339.     public int getIPadBottom()
  340.     {
  341.         return iyPadBottom;
  342.     }
  343.  
  344.     /**
  345.      * Sets the border padding amounts.
  346.      * @param t top pad amount
  347.      * @param b bottom pad amount
  348.      * @param l left pad amount
  349.      * @param r right pad amount
  350.      */
  351.     public void setPadding(int t, int b, int l, int r)
  352.     {
  353.         padtop    = t;
  354.         padbottom = b;
  355.         padleft   = l;
  356.         padright  = r;
  357.         sizepanel(true);
  358.         invalidate();
  359.     }
  360.  
  361.     /**
  362.      * Gets the current side inset border padding amount.
  363.      * @return side inset border pad amount
  364.      * @see #setIPadSides
  365.      */
  366.     public int getIPadSides()
  367.     {
  368.         return ixPad;
  369.     }
  370.  
  371.     /**
  372.      * Sets the current border label.
  373.      * @param label border label.  If null, label is removed
  374.      * @see #getLabel
  375.      */
  376.     public void setLabel(String s)
  377.     {
  378.         label = (s != null && s.length() == 0) ? null : s;
  379.         sizepanel(true);
  380.         invalidate();
  381.     }
  382.  
  383.     /**
  384.      * Gets the current border label.
  385.      * @see #setLabel
  386.      */
  387.     public String getLabel()
  388.     {
  389.         return label;
  390.     }
  391.  
  392.     /**
  393.      * Sets the current border color, and optionally the border label color.
  394.      * @param clr new border color
  395.      * @param useForLabel if true use the new border color for the label color
  396.      * as well
  397.      * @see #getBorderColor
  398.      */
  399.     public void setBorderColor(Color clr, boolean useForLabel)
  400.     {
  401.         borderColor = clr;
  402.  
  403.         if (useForLabel)
  404.         {
  405.             labelColor = clr;
  406.         }
  407.  
  408.         invalidate();
  409.     }
  410.  
  411.     /**
  412.      * Sets the current border color.
  413.      * @param clr new border color
  414.      * @see #getBorderColor
  415.      */
  416.     public void setBorderColor(Color clr)
  417.     {
  418.         setBorderColor(clr, false);
  419.     }
  420.  
  421.     /**
  422.      * Gets the current border color.
  423.      * @return current border color
  424.      * @see #setBorderColor
  425.      */
  426.     public Color getBorderColor()
  427.     {
  428.         return borderColor;
  429.     }
  430.  
  431.     /**
  432.      * Sets the border label alignment.
  433.      * @param alignment new border label alignment
  434.      * @see #getAlignStyle
  435.      * @see AlignStyle#ALIGN_LEFT
  436.      * @see AlignStyle#ALIGN_CENTERED
  437.      * @see AlignStyle#ALIGN_RIGHT
  438.      */
  439.     public void setAlignStyle(int alignment)
  440.     {
  441.         labelAlignment = alignment;
  442.         sizepanel(true);
  443.         invalidate();
  444.     }
  445.  
  446.     /**
  447.      * Gets the current border label alignment.
  448.      * @return current border label alignment
  449.      * @see AlignStyle#ALIGN_LEFT
  450.      * @see AlignStyle#ALIGN_CENTERED
  451.      * @see AlignStyle#ALIGN_RIGHT
  452.      * @see #setAlignStyle
  453.      */
  454.     public int getAlignStyle()
  455.     {
  456.         return labelAlignment;
  457.     }
  458.  
  459.     /**
  460.      * Sets the border label color.
  461.      * @param clr new border label color
  462.      * @see #getLabelColor
  463.      */
  464.     public void setLabelColor(Color clr)
  465.     {
  466.         labelColor = clr;
  467.         invalidate();
  468.     }
  469.  
  470.     /**
  471.      * Gets the current border label color.
  472.      * @return current border label color
  473.      */
  474.     public Color getLabelColor()
  475.     {
  476.         return labelColor;
  477.     }
  478.  
  479.     /**
  480.      * Sets the border style.
  481.      * @param s new border style
  482.      * @see BevelStyle#BEVEL_RAISED
  483.      * @see BevelStyle#BEVEL_LOWERED
  484.      * @see BevelStyle#BEVEL_LINE
  485.      * @see BevelStyle#BEVEL_NONE
  486.      * @see #getBevelStyle
  487.      */
  488.     public void setBevelStyle(int s)
  489.     {
  490.         style = s;
  491.         invalidate();
  492.     }
  493.  
  494.     /**
  495.      * Gets the current border style.
  496.      * @return current border style
  497.      * @see BevelStyle#BEVEL_RAISED
  498.      * @see BevelStyle#BEVEL_LOWERED
  499.      * @see BevelStyle#BEVEL_LINE
  500.      * @see BevelStyle#BEVEL_NONE
  501.      * @see #setBevelStyle
  502.      */
  503.     public int getBevelStyle()
  504.     {
  505.         return style;
  506.     }
  507.  
  508.     /**
  509.      * Returns the amount of space used by the current border.
  510.      * This is a standard Java AWT method which gets called to determine
  511.      * the size of the current border. The returned value is the width
  512.      * of each border side in pixels.
  513.      *
  514.      * @return the current border insets
  515.      */
  516.     public void setInternalInsets(Insets insets)
  517.     {
  518.         internalInsets = insets;
  519.         sizepanel(true);
  520.         invalidate();
  521.     }
  522.  
  523.     /**
  524.      * Gets the current internal border insets.
  525.      * @return current internal border insets
  526.      * @see #setInternalInsets
  527.      */
  528.     public Insets getInternalInsets()
  529.     {
  530.         return internalInsets;
  531.     }
  532.  
  533.     /**
  534.      * Handles the laying out of components within this component.
  535.      * This is a standard Java AWT method which gets called by the AWT
  536.      * when this component is validated with the validate() method.
  537.      * 
  538.      * @see java.awt.Container#validate
  539.      */
  540.     public void layout()
  541.     {
  542.         sizepanel(false);
  543.         panel.layout();
  544.     }
  545.  
  546.     /**
  547.      * Returns the minimum dimensions to properly display this component.
  548.      * This is a standard Java AWT method which gets called to determine
  549.      * the minimum size of this component. 
  550.      *
  551.      * @see #preferredSize
  552.      */
  553.     public Dimension minimumSize()
  554.     {
  555.         return preferredSize();
  556.     }
  557.  
  558.     /**
  559.      * Returns the recommended dimensions to properly display this component.
  560.      * This is a standard Java AWT method which gets called to determine
  561.      * the recommended size of this component. 
  562.      *
  563.      * @see #minimumSize
  564.      */
  565.     public Dimension preferredSize()
  566.     {
  567.         Dimension s;
  568.  
  569.         s = panel.preferredSize();
  570.  
  571.         s.width   = Math.max(s.width, getLabelWidthMargin());
  572.         s.width  += (padleft + padright + ixPad * 2) + 1;
  573.         s.height += (getLabelTopMargin() + padbottom + iyPadTop + iyPadBottom) + 1;
  574.  
  575.         return s;
  576.     }
  577.  
  578.  
  579.     /**
  580.      * Sets the layout manager to be used to layout this container.
  581.      * This is a standard Java AWT method which gets called to specify
  582.      * which layout manager should be used to layout the components in
  583.      * standard containers.
  584.      *
  585.      * @param l the layout manager to use to layout this container's components
  586.      * (IGNORED)
  587.      * @see #getLayout
  588.      **/
  589.     public void setLayout(LayoutManager l)
  590.     {
  591.         if (panel != null)
  592.         {
  593.             panel.setLayout(l);
  594.         }
  595.     }
  596.  
  597.     /**
  598.      * Gets the current border panel layout manager.
  599.      * @return current LayoutManager of border panel
  600.      * @see #setLayout
  601.      */
  602.     public LayoutManager getLayout()
  603.     {
  604.         return panel.getLayout();
  605.     }
  606.  
  607.     /**
  608.      * Adds a component to the end of this container.
  609.      * This is a standard Java AWT method which gets called to add a
  610.      * component to a container. The specified component is added to
  611.      * the end of this container.
  612.      *
  613.      * @param comp the component to add
  614.      * @return the added component
  615.      * @see #remove
  616.      */
  617.     public Component add(Component c)
  618.     {
  619.         return panel.add(c);
  620.     }
  621.  
  622.     /**
  623.      * Adds a component to the end of this container and to the layout manager.
  624.      * This is a standard Java AWT method which gets called to add a
  625.      * component to a container. The specified component is added to
  626.      * the end of this container, and also added to this container's
  627.      * layout manager with the given name.
  628.      *
  629.      * @param name the positioning directive for the layout manager
  630.      * @param comp the component to add
  631.      * @return the added component
  632.      * @see #remove
  633.      */
  634.     public Component add(String s, Component c)
  635.     {
  636.         return panel.add(s, c);
  637.     }
  638.  
  639.     /**
  640.      * Inserts a component into this container at the given position.
  641.      * This is a standard Java AWT method which gets called to add a
  642.      * component to a container. The specified component is added to
  643.      * this container at the given zero-relative position index. A
  644.      * position index of -1 appends the component to the end.
  645.      *
  646.      * @param comp the component to add
  647.      * @param pos the zero-relative index at which to add the component or -1 for end
  648.      * @return the added component
  649.      * @see #remove
  650.      */
  651.     public Component add(Component c, int pos)
  652.     {
  653.         if (c == panel)
  654.         {
  655.             return super.add(c, pos);
  656.         }
  657.  
  658.         return panel.add(c, pos);
  659.     }
  660.  
  661.     /**
  662.      * Removes the specified component from this container.
  663.      * This is a standard Java AWT method which gets called to remove a
  664.      * component from a container. When this happens the component's 
  665.      * removeNotify() will also get called to indicate component removal.
  666.      * 
  667.      * @param c the component to remove
  668.      * @see #removeAll
  669.      * @see #add
  670.      */
  671.     public void remove(Component c)
  672.     {
  673.         panel.remove(c);
  674.     }
  675.  
  676.     /**
  677.      * Removes all the components from this container.
  678.      * This is a standard Java AWT method which gets called to remove all
  679.      * the components from a container. When this happens each component's 
  680.      * removeNotify() will also get called to indicate component removal.
  681.      * 
  682.      * @see #remove
  683.      * @see #add
  684.      */
  685.     public void removeAll()
  686.     {
  687.         panel.removeAll();
  688.     }
  689.  
  690.     /**
  691.      * Moves and/or resizes this component.
  692.      * This is a standard Java AWT method which gets called to move and/or 
  693.      * resize this component. Components that are in containers with layout
  694.      * managers should not call this method, but rely on the layout manager
  695.      * instead.
  696.      * 
  697.      * @param x horizontal position in the parent's coordinate space
  698.      * @param y vertical position in the parent's coordinate space
  699.      * @param width the new width
  700.      * @param height the new height
  701.      */
  702.     public void reshape(int x, int y, int width, int height)
  703.     {
  704.         super.reshape(x, y, width, height);
  705.         sizepanel(false);
  706.     }
  707.  
  708.     /**
  709.      * Handles redrawing of this component on the screen.
  710.      * This is a standard Java AWT method which gets called by the Java
  711.      * AWT (repaint()) to handle repainting this component on the screen.
  712.      * The graphics context clipping region is set to the bounding rectangle
  713.      * of this component and its <0,0> coordinate is this component's 
  714.      * top-left corner.
  715.      * Typically this method paints the background color to clear the
  716.      * component's drawing space, sets graphics context to be the foreground
  717.      * color, and then calls paint() to draw the component.
  718.      *
  719.      * @param g the graphics context
  720.      * @see java.awt.Component#repaint
  721.      * @see #paint
  722.      */
  723.     public void update(Graphics g)
  724.     {
  725.         Dimension s;
  726.         Insets insets;
  727.  
  728.         s      = size();
  729.         insets = insets();
  730.  
  731.         g.setColor(getBackground());
  732.  
  733.         if (insets.left > 0)
  734.         {
  735.             g.fillRect(0, 0, insets.left, s.height);
  736.         }
  737.  
  738.         if (insets.top > 0)
  739.         {
  740.             g.fillRect(0, 0, s.width, insets.top);
  741.         }
  742.  
  743.         if (insets.bottom > 0)
  744.         {
  745.             g.fillRect(0, s.height-insets.bottom, s.width, insets.bottom);
  746.         }
  747.  
  748.         if (insets.right > 0)
  749.         {
  750.             g.fillRect(s.width-insets.right, 0, insets.right, s.height);
  751.         }
  752.  
  753.         paint(g);
  754.         panel.repaint();
  755.     }
  756.  
  757.     /**
  758.      * Returns the number of components in this container.
  759.      * This is a standard Java AWT method which gets called to return a count
  760.      * of the components within this container.
  761.      *
  762.      */
  763.     public int countComponents()
  764.     {
  765.         return panel.countComponents();
  766.     }
  767.  
  768.     /**
  769.      * Gets the component at a specific zero-relative component index.
  770.      * This is a standard Java AWT method which gets called to return
  771.      * the component at a specific position.
  772.      *
  773.      * @param i the zero-relative index of the component to retrieve
  774.      * @return the component at the given index
  775.      * @exception java.lang.ArrayIndexOutOfBoundsException if the given 
  776.      * component index does not exist
  777.      * see getComponents
  778.      */
  779.     public Component getComponent(int i)
  780.     {
  781.         return panel.getComponent(i);
  782.     }
  783.  
  784.     /**
  785.      * Returns all of the components in this container.
  786.      * This is a standard Java AWT method which gets called to return
  787.      * an array of all of the components in this container.
  788.      *
  789.      * @return an array of components in this container
  790.      */
  791.     public Component[] getComponents()
  792.     {
  793.         return panel.getComponents();
  794.     }
  795.  
  796.     /**
  797.      * Returns the current border insets.
  798.      */
  799.     public Insets insets()
  800.     {
  801.         int h;
  802.         Insets insets;
  803.  
  804.         h      = getLabelTopMargin();
  805.         insets = getInternalInsets();
  806.  
  807.         return new Insets(h + insets.top, insets.left, insets.bottom, insets.right);
  808.     }
  809.  
  810.  
  811.     /**
  812.      * Sizes the border panel from the padding, inset, and label margin information.
  813.      * @param force force reshape even if the overall size hasn't changed
  814.      */
  815.     void sizepanel(boolean force)
  816.     {
  817.         Dimension s;
  818.  
  819.         s = size();
  820.  
  821.         if (force || oldSize.width != s.width || oldSize.height != s.height)
  822.         {
  823.             oldSize = size();
  824.             panel.reshape(padleft + ixPad,
  825.                   getLabelTopMargin() + iyPadTop,
  826.                   oldSize.width - padright - padleft - ixPad * 2 - 1,
  827.                   oldSize.height - padbottom - getLabelTopMargin() - iyPadBottom - iyPadTop - 1);
  828.         }
  829.     }
  830.  
  831.     /**
  832.      * Paints this component using the given graphics context.
  833.      * This is a standard Java AWT method which typically gets called
  834.      * by the AWT to handle painting this component. It paints this component
  835.      * using the given graphics context. The graphics context clipping region
  836.      * is set to the bounding rectangle of this component and its <0,0>
  837.      * coordinate is this component's top-left corner.
  838.      * 
  839.      * @param g the graphics context used for painting
  840.      * @see java.awt.Component#repaint
  841.      * @see #update
  842.      */
  843.     public void paint(Graphics g)
  844.     {
  845.         sizepanel(false);
  846.         g.setColor(getBackground());
  847.         draw(g);
  848.     }
  849.  
  850.     /**
  851.      * Draws the border panel.
  852.      * @param g current graphics object
  853.      */
  854.     void draw(Graphics g)
  855.     {
  856.         Dimension s;
  857.         int delta;
  858.         FontMetrics fm;
  859.         int x;
  860.         int y;
  861.         int w;
  862.         int h;
  863.  
  864.         s = size();
  865.         delta = padtop;
  866.         fm = getFontMetrics(getFont());
  867.  
  868.         g.clipRect(0, 0, s.width, s.height);
  869.  
  870.         if (label != null && fm != null)
  871.         {
  872.             delta = (fm.getAscent()  + fm.getDescent() + padtop) / 2;
  873.         }
  874.  
  875.         x = padleft;
  876.         y = delta;
  877.         w = s.width - padleft - padright - 1;
  878.         h = s.height - 1 - delta - padbottom;
  879.  
  880.         drawBorder(g, x, y, w, h);
  881.  
  882.         drawLabel(g, fm);
  883.     }
  884.  
  885.     /**
  886.      * Draws the border panel border.
  887.      * @param g current graphics object
  888.      * @param x x coordinate of panel object
  889.      * @param y y coordinate of panel object
  890.      * @param w w width of panel object
  891.      * @param h h height of panel object
  892.      */
  893.     void drawBorder(Graphics g, int x, int y, int w, int h)
  894.     {
  895.         switch(style)
  896.         {
  897.             case BEVEL_RAISED :
  898.             {
  899.                 g.setColor(Color.white);
  900.                 g.drawLine(x, y, x + w, y);
  901.                 g.drawLine(x, y, x, y + h);
  902.                 g.setColor(Color.gray);
  903.                 g.drawLine(x, y + h, x + w, y + h);
  904.                 g.drawLine(x + w, y, x + w, y + h);
  905.                 break;
  906.             }
  907.             case BEVEL_LOWERED :
  908.             {
  909.                 g.setColor(Color.gray);
  910.                 g.drawLine(x, y, x + w, y);
  911.                 g.drawLine(x, y, x, y + h);
  912.                 g.setColor(Color.white);
  913.                 g.drawLine(x, y + h, x + w, y + h);
  914.                 g.drawLine(x + w, y, x + w, y + h);
  915.                 break;
  916.             }
  917.             case BEVEL_NONE :
  918.             {
  919.                 break;
  920.             }
  921.             case BEVEL_LINE :
  922.             {
  923.                 g.setColor(borderColor);
  924.                 g.drawRect(x, y, w, h);
  925.                 break;
  926.             }
  927.             default:
  928.             {
  929.                 g.setColor(borderColor);
  930.                 g.drawRect(x, y, w, h);
  931.                 break;
  932.             }
  933.         }
  934.     }
  935.  
  936.     /**
  937.      * Draws the border panel label.
  938.      * @param g current graphics object
  939.      * @param fm FontMetrics of border panel label
  940.      */
  941.     void drawLabel(Graphics g, FontMetrics fm)
  942.     {
  943.         if (label != null && (fm != null))
  944.         {
  945.             int fWidth;
  946.             Dimension s;
  947.             int stringWidth;
  948.             int ascent;
  949.             int descent;
  950.             int x;
  951.             int y;
  952.             int h;
  953.  
  954.             fWidth = 10;
  955.             s = size();
  956.  
  957.             if (getFont().getSize() > fWidth)
  958.             {
  959.                 fWidth = fWidth + getFont().getSize() / 2;
  960.             }
  961.  
  962.             stringWidth = fm.stringWidth(label);
  963.             ascent      = fm.getAscent();
  964.             descent     = fm.getDescent();
  965.  
  966.             switch(labelAlignment)
  967.             {
  968.                 case Label.CENTER:
  969.                 {
  970.                     x = (s.width - stringWidth) / 2;
  971.                     break;
  972.                 }
  973.                 case Label.RIGHT:
  974.                 {
  975.                     x = s.width - fWidth - (stringWidth + (labelpadx + labelipadx) / 2);
  976.                     break;
  977.                 }
  978.                 case Label.LEFT:
  979.                 {
  980.                 }
  981.                 default:
  982.                 {
  983.                     x = fWidth + (labelpadx + labelipadx) / 2;
  984.                 }
  985.             }
  986.  
  987.             //y = ascent + padtop;            //This line is useless. 12/17/96 Levi Brown
  988.             h = ascent + descent+padtop;
  989.  
  990.             y = (fWidth - h) / 2 + (padtop + ascent);
  991.             //h = fWidth;                    //I like h better before... 12/17/96 Levi Brown
  992.  
  993.             g.setColor(getBackground());
  994.             //Commented out the line below, to add my own below it.  12/17/96 Levi Brown
  995.             //g.fillRect(x - labelipadx / 2, 0, stringWidth + labelipadx, h);
  996.             g.fillRect(x - labelipadx / 2, y - 1 - ascent - padtop/2, stringWidth + labelipadx, h);
  997.             g.setColor(labelColor);
  998.             g.drawString(label, x, y - 1);
  999.         }
  1000.     }
  1001.  
  1002.     /**
  1003.      * Returns the current label top margin.
  1004.      */
  1005.     protected int getLabelTopMargin()
  1006.     {
  1007.         int  top;
  1008.         Font font;
  1009.  
  1010.         if (label == null)
  1011.         {
  1012.             return padtop;
  1013.         }
  1014.  
  1015.         top  = padtop;
  1016.         font = getFont();
  1017.  
  1018.         if (font != null)
  1019.         {
  1020.             FontMetrics fm;
  1021.  
  1022.             fm  = getFontMetrics(font);
  1023.             top = fm.getAscent() + fm.getDescent() + padtop + 0;
  1024.         }
  1025.  
  1026.         return top;
  1027.     }
  1028.  
  1029.     /**
  1030.      * Returns the current label width margin.
  1031.      */
  1032.     int getLabelWidthMargin()
  1033.     {
  1034.         if (label == null)
  1035.         {
  1036.             return 0;
  1037.         }
  1038.  
  1039.         int w;
  1040.         Font font;
  1041.  
  1042.         w = 2 + internalInsets.left + internalInsets.right;
  1043.         font = getFont();
  1044.  
  1045.         if (font != null)
  1046.         {
  1047.             FontMetrics fm;
  1048.  
  1049.             fm = getFontMetrics(font);
  1050.             w  = Math.max(w, 2 + fm.stringWidth(label) + labelpadx + labelipadx);
  1051.         }
  1052.  
  1053.         return w;
  1054.     }
  1055.  
  1056.     /**
  1057.      * Sets this component's background color.
  1058.      * This is a standard Java AWT method which gets called to change
  1059.      * the background color of this component.
  1060.      *
  1061.      * @param c the new background color
  1062.      * @see java.awt.Component#getBackground
  1063.      */
  1064.     public void setBackground(Color c)
  1065.     {
  1066.         super.setBackground(c);
  1067.         panel.setBackground(c);
  1068.     }
  1069. }
  1070.  
  1071.